package com.aierjun.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.aierjun.myapplication.utils.MySharedPreferences;
import com.aierjun.myapplication.view.DateDialog;
import com.aierjun.myapplication.view.MyDelWeightDialog;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Ani_aierJun on 2017/9/12.
 */

public class CarMessageAddActivity extends Activity implements View.OnClickListener {
    private ImageView back;
    private TextView save, commit;
    private EditText car_type, car_invoice_units, car_invoice_number, car_type_mes, car_carriage_number, car_chassis_number, car_engine_number, car_color;
    private EditText  car_gps_number, car_gps_mes;
    private TextView gps_get_state, gps_cancel_state;
    private TextView car_register_time,car_gps_install_time ,car_gps_open_time ,car_gps_state ;

    private MyDelWeightDialog myDelWeightDialog;

    private List<String> name;
    private List<String> data;

    public final static String MySharedPreferencesNAME = "CarMessage";

    private DateDialog dateDialog;
    private String dateString;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_message_add);
        initData();
        findView();
        initView();
    }

    private void findView() {
        back = (ImageView) findViewById(R.id.back);
        save = (TextView) findViewById(R.id.save);
        commit = (TextView) findViewById(R.id.commit);
        car_type = (EditText) findViewById(R.id.car_message_car_type_ed);
        car_invoice_units = (EditText) findViewById(R.id.car_message_car_invoice_units_ed);
        car_invoice_number = (EditText) findViewById(R.id.car_message_car_invoice_number_ed);
        car_type_mes = (EditText) findViewById(R.id.car_message_car_type_mes_ed);
        car_carriage_number = (EditText) findViewById(R.id.car_message_car_carriage_number_ed);
        car_chassis_number = (EditText) findViewById(R.id.car_message_car_chassis_number_ed);
        car_engine_number = (EditText) findViewById(R.id.car_message_car_engine_number_ed);
        car_color = (EditText) findViewById(R.id.car_message_car_color_ed);
        car_register_time = (TextView) findViewById(R.id.car_message_car_register_time_ed);

        car_gps_install_time = (TextView) findViewById(R.id.car_message_car_gps_install_time_ed);
        car_gps_number = (EditText) findViewById(R.id.car_message_car_gps_number_ed);
        car_gps_state = (TextView) findViewById(R.id.car_message_car_gps_state_ed);
        car_gps_open_time = (TextView) findViewById(R.id.car_message_car_gps_open_time_ed);
        car_gps_mes = (EditText) findViewById(R.id.car_message_car_gps_mes_ed);

        gps_get_state = (TextView) findViewById(R.id.gps_get_state);
        gps_cancel_state = (TextView) findViewById(R.id.gps_cancel_state);
    }

    private void initView() {
        back.setOnClickListener(this);
        save.setOnClickListener(this);
        commit.setOnClickListener(this);
        gps_get_state.setOnClickListener(this);
        gps_cancel_state.setOnClickListener(this);
        car_register_time.setOnClickListener(this);
        car_gps_install_time.setOnClickListener(this);

        showLocal();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                myDelWeightDialog = new MyDelWeightDialog(this) {
                    @Override
                    protected void checkLeft(TextView textView) {
                        //取消
                        Log.d("Jun ", "取消");
                        finish();
                    }

                    @Override
                    protected void checkRight(TextView textView) {
                        //保存
                        Log.d("Jun ", "保存");
                        savaLocalData();
                        finish();
                    }
                };
                myDelWeightDialog.setDelMes("当前未保存，确定继续退出？");
                myDelWeightDialog.setLeftMes("取消");
                myDelWeightDialog.setRightMes("保存");
                myDelWeightDialog.show();

                break;
            case R.id.save://功能更改，返回时提示存储
                //保存
                savaLocalData();
                break;
            case R.id.commit:
                //提交
                break;
            case R.id.gps_get_state:
                //获取gps状态
                break;
            case R.id.gps_cancel_state:
                //撤销gps录入
                break;
            case R.id.car_message_car_gps_install_time_ed:
                showDate((TextView) v);
                break;
            case R.id.car_message_car_register_time_ed:
                showDate((TextView) v);
                break;
        }
    }

    private void initData() {
        if (name == null)
            name = new ArrayList<>();
        if (data == null)
            data = new ArrayList<>();
        name.add("car_type");
        name.add("car_invoice_units");
        name.add("car_invoice_number");
        name.add("car_type_mes");
        name.add("car_carriage_number");
        name.add("car_chassis_number");
        name.add("car_engine_number");
        name.add("car_color");
        name.add("car_register_time");

        name.add("car_gps_install_time");
        name.add("car_gps_number");
        name.add("car_gps_state");
        name.add("car_gps_open_time");
        name.add("car_gps_mes");
    }

    /*
    * 从本地得到数据
    * */
    private List<String> getLocalData() {
        data = MySharedPreferences.getStringList(this, MySharedPreferencesNAME, name);
        return data;
    }

    /*
    * 从服务器得到数据
    * */
    private List<String> getData() {
        //服务器获取数据和解析
        return data;
    }

    private void showLocal() {
        if (getLocalData().size() > 0) {
            myDelWeightDialog = new MyDelWeightDialog(this) {
                @Override
                protected void checkLeft(TextView textView) {
                    //取消
                    Log.d("Jun ", "取消");
                    myDelWeightDialog.dismiss();
                    myDelWeightDialog = null;
                }

                @Override
                protected void checkRight(TextView textView) {
                    //保存
                    Log.d("Jun ", "确认");
                    setData();
                    myDelWeightDialog.dismiss();
                    myDelWeightDialog = null;
                }
            };
            myDelWeightDialog.setDelMes("是否自动填充上次的数据？");
            myDelWeightDialog.setLeftMes("取消");
            myDelWeightDialog.setRightMes("确认");
            myDelWeightDialog.show();
        }
    }

    private void setData() {
        if (data.size() > 0)
            car_type.setText(data.get(0));
        if (data.size() > 1)
            car_invoice_units.setText(data.get(1));
        if (data.size() > 2)
            car_invoice_number.setText(data.get(2));
        if (data.size() > 3)
            car_type_mes.setText(data.get(3));
        if (data.size() > 4)
            car_carriage_number.setText(data.get(4));
        if (data.size() > 5)
            car_chassis_number.setText(data.get(5));
        if (data.size() > 6)
            car_engine_number.setText(data.get(6));
        if (data.size() > 7)
            car_color.setText(data.get(7));
        if (data.size() > 8)
            car_register_time.setText(data.get(8));

        if (data.size() > 9)
            car_gps_install_time.setText(data.get(9));
        if (data.size() > 10)
            car_gps_number.setText(data.get(10));
        if (data.size() > 11)
            car_gps_state.setText(data.get(11));
        if (data.size() > 12)
            car_gps_open_time.setText(data.get(12));
        if (data.size() > 13)
            car_gps_mes.setText(data.get(13));
    }

    private void savaLocalData(){
        data.clear();
        data.add(car_type.getText().toString());
        data.add(car_invoice_units.getText().toString());
        data.add(car_invoice_number.getText().toString());
        data.add(car_type_mes.getText().toString());
        data.add(car_carriage_number.getText().toString());
        data.add(car_chassis_number.getText().toString());
        data.add(car_engine_number.getText().toString());
        data.add(car_color.getText().toString());
        data.add(car_register_time.getText().toString());
        data.add(car_gps_install_time.getText().toString());
        data.add(car_gps_number.getText().toString());
        data.add(car_gps_state.getText().toString());
        data.add(car_gps_open_time.getText().toString());
        data.add(car_gps_mes.getText().toString());
        MySharedPreferences.saveStringList(this,MySharedPreferencesNAME,name,data);
        Log.d("Jun",math(car_carriage_number.getText().toString())+"");
    }

    /*
    * 验证数据 验证序列号
    * A-Z
    * a-z
    * 0-9
    * 通过
    * */
    public boolean math(String s){
        return s.matches("^[0-9A-Za-z]+$");
    }


    public void showDate(final TextView date){
        dateDialog = new DateDialog(this);
        dateDialog.setCallBackDate(new DateDialog.CallBackDate() {
            @Override
            public void callBackDateString(String d) {
                Log.d("Jun date ", d);
                dateString = d;
            }

            @Override
            public void clickLeft(View view) {
                Log.d("Jun dateDialog ", "clickLeft");
                dateDialog.dismiss();
                dateString = null;
            }

            @Override
            public void clickRight(View view) {
                Log.d("Jun dateDialog ", "clickRight");
                if (dateString == null || dateString.equals("")) {
                    SimpleDateFormat format = new SimpleDateFormat(
                            "yyyy/MM/dd");
                    date.setText(format.format(System.currentTimeMillis()));
                } else {
                    date.setText(dateString);
                }
                dateDialog.dismiss();
                dateString = null;
            }
        });
        dateDialog.showGravity(Gravity.CENTER).show();
    }
}
